var _selectedItem;
var _onSelectFunc;
var _noItemsMessage;

var KEY_UP = 38;
var KEY_DOWN = 40;
var KEY_HOME = 36;
var KEY_END = 35;

function On( e )
{
GetSrc( e );

if( e != _selectedItem )
{
if (e.className.indexOf(" ms-crm-GlowOn") < 0)
e.className += " ms-crm-GlowOn";
}
}

function Off( e )
{
GetSrc( e );

if( e != _selectedItem )
{
e.className = e.className.replace(" ms-crm-GlowOn", "");
}
}

function SelectItem( e )
{
    GetSrc( e );

    if( e != _selectedItem )
    {
        e.className = e.className.replace(" ms-crm-GlowOn", "");
        if (e.className.indexOf(" ms-crm-SelectOn") < 0)
        e.className += " ms-crm-SelectOn";
        e.firstChild.checked=true;
        e.firstChild.focus();
        if( _selectedItem )
        {
            _selectedItem.className = _selectedItem.className.replace(" ms-crm-GlowOn", "");
            _selectedItem.className = _selectedItem.className.replace(" ms-crm-SelectOn", "");


            if (_selectedItem.firstChild)
            {
                _selectedItem.firstChild.checked=false;
            }
        }
        _selectedItem = e;



        if (_onSelectFunc)
        {
            _onSelectFunc(e);
        }
    }
}

function GetSrc(e)
{
if (e.tagName != "LI")
{
e = e.parentElement;
}
}

function PageOnLoad()
{
if (typeof(tblItems) != "undefined")
{
var oItems = tblItems.all.tags("li");
if( oItems != null && oItems.length > 0 )
{


SelectItem( oItems[0] );
}
else
{

crmForm.butBegin.disabled = true;



tblItems.outerHTML = GetEmptyMessageHtml();
}
}
}

window.attachEvent('onload', PageOnLoad);

function document.onkeydown()
{
try
{
switch (event.keyCode)
{
case KEY_UP:
if (typeof(_selectedItem.previousSibling.item) != "undefined")
{
SelectItem(_selectedItem.previousSibling);
}
break;

case KEY_DOWN:
if (typeof(_selectedItem.nextSibling.item) != "undefined")
if (_selectedItem.nextSibling.item != undefined)
{
SelectItem(_selectedItem.nextSibling);
}
break;
case KEY_HOME:
if (typeof(_selectedItem.parentElement.firstChild.item) != "undefined")
{
SelectItem(_selectedItem.parentElement.firstChild);
}
break;
case KEY_END:
if (typeof(_selectedItem.parentElement.lastChild.item) != "undefined")
{
SelectItem(_selectedItem.parentElement.lastChild);
}
break;
}
}
catch(e)
{
}
}


function LoadOptionsForLanguage(languageCode, listType)
{
var oCommand = new RemoteCommand("DialogList", "RetrieveListHtml");
oCommand.SetParameter("languageCode", languageCode);
oCommand.SetParameter("listType", listType);

var oResult = oCommand.Execute();
if (typeof(oResult.ReturnValue) == "string")
{
ItemList.innerHTML = oResult.ReturnValue;
PageOnLoad();
crmForm.butBegin.disabled = false;
}
else
{
ItemList.innerHTML = GetEmptyMessageHtml();
crmForm.butBegin.disabled = true;
}
}


function GetEmptyMessageHtml()
{
if( !IsNull(_noItemsMessage) && _noItemsMessage.length > 0 )
{
return "<table style='width:100%; height:100%;'><tr><td style='height:100%;text-align:center;color:#999999;'>" + CrmEncodeDecode.CrmHtmlEncode(_noItemsMessage) + "<br><br></td></tr></table>";
}
else
{
return "";
}
}